﻿<%@ Page Language="C#" MasterPageFile="../../masterpages/umbracoDialog.Master"
    AutoEventWireup="true" CodeBehind="UploadFiles.aspx.cs" Inherits="Dascoba.Umb.FileManager.Dialogs.UploadFiles"
    ValidateRequest="false" %>

<%@ Register TagPrefix="umb" Namespace="umbraco.uicontrols" Assembly="controls" %>
<asp:Content ID="Content2" ContentPlaceHolderID="head" runat="server">
    <link href="../js/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
    <script src="../js/uploadify/swfobject.js" type="text/javascript"></script>
    <script src="../js/uploadify/jquery.uploadify.v2.1.0.min.js" type="text/javascript"></script>
</asp:Content>
<asp:Content ID="Content1" ContentPlaceHolderID="body" runat="server">
    <umb:Pane ID="pane_upload" runat="server">
        <umb:PropertyPanel ID="pp_upload" runat="server">
            <p style="float: right">
                <a id="clearQueue" style="display:none;" href="javascript:$('#fileInput').uploadifyClearQueue();$('#clearQueue').hide();"><%=umbraco.ui.Text("filemanbuttons", "clearFailed")%></a>&nbsp;&nbsp;
                <a id="cancel" href="javascript:parent.refreshNode();parent.closeModal();"><%=umbraco.ui.Text("filemanbuttons", "close") %></a>&nbsp;&nbsp;
            </p>
            <input id="fileInput" name="fileInput" type="file" />
            <p></p>
            <asp:Panel ID="pnlFileQueue" runat="server" Width="100%">
                <div id="divFileQueue" style="height: 240px; width: 100%; overflow:auto;" />
            </asp:Panel>
        </umb:PropertyPanel>
    </umb:Pane>
    <script type="text/javascript">

        var uploaded = 0;
        var failed = 0;
        var errormsg = '';

        $(document).ready(function () {
            $('#fileInput').uploadify({
                'uploader': '../js/uploadify/uploadify.swf',
                'script': 'UploadHandler.ashx',
                'scriptData': { 'uploadPath': '<% =this.FolderUrl %>' },
                'cancelImg': '../images/cancel.png',
                'buttonText': '<%=umbraco.ui.Text("filemanbuttons", "uploadFiles") %>...',
                'folder': '<% =this.FolderUrl %>',

                'auto': true,               // auto-start upload after select (true|false)
                'multi': true,              // allow multiple files (true|false)
                'fileDesc': '<%=umbraco.ui.Text("fileman", "allFiles") %>',    // allowed extensions description
                'fileExt': '*.*',           // allowed extensions (comma delimited)
                'queueID': 'divFileQueue',  // queue element
                'queueSizeLimit': 100,      // upload count limit
                'sizeLimit': 200000000,     // max file size in bytes

                'onError': function (event, queueID, fileObj, errorObj) {
                    handleError(queueID, fileObj.name, 'client-side: ' + errorObj.info);
                },
                'onComplete': function (event, queueID, fileObj, response, data) {
                    if (response == 'success') {
                        uploaded = uploaded + 1;
                    }
                    else {
                        handleError(queueID, fileObj.name, 'server-side: ' + response);
                    }
                },
                'onAllComplete': function (event, queueID, fileObj, response, data) {
                    if (failed > 0) {
                        errormsg = '\r\n' + failed + ' file(s) <%=umbraco.ui.Text("fileman", "failed") %>.\r\n\r\n' + errormsg;
                        $('#clearQueue').show();
                    }

                    window.alert(uploaded + '<%=umbraco.ui.Text("fileman", "uploadComplete") %>' + errormsg);
                    upload = 0; failed = 0; errormsg = '';

                    // Close dialog after all uploads complete
                    //parent.closeModal();
                }
            });
        });

        function handleError(queueID, filename, error) {

            // build error message
            errormsg = errormsg + '\r\n' + filename + ' failed (' + error + ')';

            // cancel item otherwise Uploadify wont upload anymore files.
            $("#uploadify").uploadifyCancel(queueID);

            // add to fail count
            failed = failed + 1;
        }
    </script>
</asp:Content>
